package com.rahwi.rahwi_server.Service.impl;

import com.rahwi.rahwi_server.DAO.PostDAO;
import com.rahwi.rahwi_server.Entity.Post;
import com.rahwi.rahwi_server.Service.Interface.IPostService;
import com.rahwi.rahwi_server.vo.params.PageParams;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.Optional;
import java.util.List;

@Service
public class PostService implements IPostService {

    @Autowired
    private PostDAO postDAO;

    /**
     *
     * @param pageParams
     * @return Post[]
     * 构造PageRequest对象，调用postDao.findAll(PageRequest)方法
     */
    @Override
    public List<Post> getPostByPage(PageParams pageParams) {
        PageRequest pageable = PageRequest.of(pageParams.getPage(), pageParams.getSize());
        Page<Post> postPage = postDAO.findAll(pageable);
//        return PostConverter.toVoPost(postPage.getContent(), this::toBase).toArray(new BasePost[0]);
        return postPage.getContent();
    }

    /**
     *
     * @param pid
     * @return Post
     * 通过构造specification对象作为查询参数，调用postDao的方法
     */
    @Override
    public Post getPostById(int pid) {
        Specification<Post> specification = new Specification<Post>() {
            @Override
            public Predicate toPredicate(Root<Post> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                return criteriaBuilder.equal(root.get("Pid"),pid);
            }
        };
        Optional<Post> one = postDAO.findOne(specification);

        return one.orElse(null);
    }

    /**
     *
     * @param tag
     * @param pageParams
     * @return Post[]
     * 构造specification和pageRequest对象调用findAll查询
     */
    @Override
    public List<Post> getPostByTagAndPage(String tag, PageParams pageParams) {
        Specification<Post> specification = new Specification<Post>() {
            @Override
            public Predicate toPredicate(Root<Post> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                return criteriaBuilder.equal(root.get("Tag"),tag);
            }
        };
        PageRequest pageRequest = PageRequest.of(pageParams.getPage(),pageParams.getSize());
        Page<Post> postPage = postDAO.findAll(specification,pageRequest);
        return postPage.getContent();
    }

    /**
     * 实现添加post
     * @param post
     */
    @Override
    public void addPost(Post post) {
        //这里应该抛异常：用户id不存在异常

        postDAO.save(post);
    }

    /**
     * 修改
     */
    @Override
    public void modifyPost(Post post) {
        //这里应该抛异常：用户id不存在异常

        postDAO.save(post);
    }

    /**
     * 通过postid删除
     * @param pid
     */
    @Override
    public void deletePostById(int pid) {
        postDAO.deleteById(pid);
    }

    @Override
    public List<Post> getPostByUidAndPage(int uid, PageParams pageParams) {
        Specification<Post> specification = new Specification<Post>() {
            @Override
            public Predicate toPredicate(Root<Post> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                return criteriaBuilder.equal(root.get("Uid"),uid);
            }
        };
        PageRequest pageRequest = PageRequest.of(pageParams.getPage(),pageParams.getSize());
        Page<Post> postPage = postDAO.findAll(specification,pageRequest);
        return postPage.getContent();
    }
}
